/* Siberian Olympiad. 2 stage.
  Problemset: http://agul-problems1.googlecode.com/svn/trunk/2010-2011/wso/stage2/wso_2stage.pdf
  Problem 10.
  Points: 70
*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
#include <cstring>
using namespace std;
class new_variable {
private:
	int task, id;
public:
	void print () const {
		if (task>2) printf("%c%d ",task,id+1);
		else printf("%d ",task);
	}
	bool operator< (const new_variable rug) const {
		if (task==rug.task) return id<rug.id;
		return task<rug.task;
	}
	bool operator== (const new_variable rug) const {
		return task==rug.task && id==rug.id;
	}
	bool operator^ (const new_variable rug) const {
		return id==-1 && rug.id==-1 && rug.task!=task;
	}
	new_variable (int new_variable_task, int new_variable_id) {
		task=new_variable_task;
		id=new_variable_id;
	}
	new_variable () {};
};
int length[30];
void add (char c, vector <new_variable> *part) {
	if (c=='0' || c=='1') {
		part->push_back(new_variable(c-'0',-1));
		return;
	}
	for (int i=0; i<length[c-'a']; i++) {
		part->push_back(new_variable(c,i));
	}
	return;
}
class longs {
private:
	short chislo[3600], size;
public:
	bool operator< (const longs rug) const {
		if (size==rug.size) {
			for (int i=size-1; i>=0; i--) {
				if (chislo[i]!=rug.chislo[i]) return chislo[i]<rug.chislo[i];
			}
			return false;
		}
		return size<rug.size;
	}
	bool operator== (const longs rug) const {
		if (size==rug.size) {
			for (int i=size-1; i>=0; i--) {
				if (chislo[i]!=rug.chislo[i]) return false;
			}
			return true;
		}
		return false;
	}
	void print() {
		for (int i=size-1; i>=0; i--) cout << chislo[i];
		cout << endl;
	}
	short length () {
		return size;
	}
	longs() {
		size=1;
		for (int i=0; i<3600; i++) chislo[i]=0;
	}
	longs (char *s) {
		size=strlen(s);
		for (int i=0; i<size; i++)
			chislo[i]=s[size-i-1]-'0';
		for (int i=size; i<3600; i++)
			chislo[i]=0;
	}
	longs (int a) {
		size=0;
		while (a!=0) {
			chislo[size++]=a % 10;
			a /= 10;
		}
		for (int i=size; i<3600; i++) chislo[i]=0;
		if (!size) size=1;
	}
	longs operator+ (const longs rug) const {
		longs res;
		for (int i=0; i<=max(rug.size,size); i++) res.chislo[i]=0;
		for (int i=0; i<max(rug.size,size); i++) {
			res.chislo[i+1]=(chislo[i]+rug.chislo[i]+res.chislo[i]) / 10;
			res.chislo[i]=(chislo[i]+rug.chislo[i]+res.chislo[i]) % 10;
		}
		res.size=max(rug.size,size);
		if (res.chislo[res.size]) res.size++;
		return res;
	}
	longs operator- (const longs rug) const {
		longs res;
		for (int i=0; i<=max(rug.size,size); i++) res.chislo[i]=0;
		short sub=0;
		for (int i=0; i<size; i++) {
			if (chislo[i]-sub-rug.chislo[i]>=0) {
				res.chislo[i]=chislo[i]-sub-rug.chislo[i];
				sub=0;
			} else {
				res.chislo[i]=chislo[i]-sub-rug.chislo[i]+10;
				sub=1;
			}
		}
		res.size=size;
		while (res.chislo[res.size-1]==0 && res.size>1) res.size--;
		return res;
	}		
};
longs get_s (int n) {
	longs res(1);
	for (int i=0; i<n; i++)
		res = res+res;
	return res;
}
int main () {
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	int x; scanf("%d",&x);
	for (int i=0; i<x; i++) {
		vector <new_variable> view_left,view_right;
		set <new_variable> was;
		was.insert(new_variable(0,-1));
		was.insert(new_variable(1,-1));
		int ispolz[30],ostat=0;
		int k; scanf("%d",&k);
		for (int i=0; i<k; i++) {
			ispolz[i]=0;
			scanf("%d",&length[i]);
			ostat+=length[i];
		}
		int l; scanf("%d",&l);
		for (int i=0; i<l;i++) {
			char c;
			do scanf("%c",&c); while (c!='1' && c!='0' && !(c>='a' && c<='z'));
			add(c,&view_left);
			if (c>='a') ispolz[c-'a']=1;
		}
		scanf("%d",&l);
		for (int i=0; i<l;i++) {
			char c;
			do scanf("%c",&c); while (c!='1' && c!='0' && !(c>='a' && c<='z'));
			add(c,&view_right);
			if (c>='a') ispolz[c-'a']=1;
		}	
		for (int i=0; i<k; i++)
			if (ispolz[i]) ostat -= length[i];
		if (view_left.size()<view_right.size())
			view_left.insert(view_left.begin(),view_right.size()-view_left.size(),new_variable(0,-1));		
		if (view_right.size()<view_left.size())
			view_right.insert(view_right.begin(),view_left.size()-view_right.size(),new_variable(0,-1));
		l=view_left.size();
		int t;
		do {
			t=0;
			for (int i=0; i<l; i++) {
				new_variable old_v,new_variable_v;
				if (view_right[i]<view_left[i]) {
					t=1;
					old_v=view_left[i];
					new_variable_v=view_right[i];
				} else {
					if (view_left[i]<view_right[i]) {
						new_variable_v=view_left[i];
						old_v=view_right[i];
						t=1;
					}
				}
					replace(view_left.begin(),view_left.end(),old_v,new_variable_v);
					replace(view_right.begin(),view_right.end(),old_v,new_variable_v);
			}
		} while (t);
		bool bad=false;
		for (int i=0; i<view_left.size(); i++) {
			if (view_left[i] ^ view_right[i]) {
				bad=true;
				break;
			}
		}
		if (bad) {
			printf("0\n");
			break;
		}
		for (int i=0; i<view_left.size(); i++) was.insert(view_left[i]);
		for (int i=0; i<view_right.size(); i++) was.insert(view_right[i]);
		(get_s(was.size()-2+ostat)).print();
	}
	return 0;
}
